package biolean.logic;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import biolean.Commons;
import biolean.Utils;

public abstract class GraphicElement implements Commons {
	protected int x;
	protected int y;
	protected int width;
	protected boolean active = false;
	protected List<GraphicElement> mConnection = new ArrayList<GraphicElement>(); 
	protected List<GraphicElement> mInPuts     = new ArrayList<GraphicElement>();
	public List<GraphicElement> getInputs(){return mInPuts;}	
	public List<GraphicElement> getNeighbours(){return mConnection; }
	protected  String type; // typ  file/command
	protected boolean mSelected;
	
	public boolean isConnected(GraphicElement element)
	{
		return mConnection.contains( element );
	}


	public GraphicElement()
	{
		
	}
	
	protected boolean mReady = true;
	public void setReady(boolean ready) {
		this.mReady = ready;
	}

	public boolean isReady() {
		return mReady;
	}
	
	
	public GraphicElement(int x, int y) {
		setXY(x, y);
	}

	public void setX(int x) {
		this.x = x;
	}

	public void setY(int y) {
		this.y = y;
	}

	public void setXY(int x, int y) {
		setX(x);
		setY(y);
	}

	public int getY() {
		return y;
	}

	public int getX() {
		return x;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public boolean getActive() {
		return active;
	}

	public void draw(Graphics g) {

	}
	
	public void setSelected(boolean sel)
	{
		mSelected =  sel;
	}
	
	public String getType() {
		return type;
	}
	

	public boolean isMouseOverCommand(int xx, int yy) {
		int x1 = getX() - COMMAND_WIDTH / 2;
		int y1 = getY() - +COMMAND_HEIGHT / 2;
		int x2 = getX() + COMMAND_WIDTH / 2;
		int y2 = getY() + COMMAND_HEIGHT / 2;

		boolean inWidth = (xx >= x1) && (xx <= x2);
		boolean inHeight = (yy >= y1) && (yy <= y2);

		return inWidth && inHeight;
	}
	
	public boolean isMouseOverFile(int xx, int yy) {
		int x1 = getX() - width / 2;
		int y1 = getY() - +FILE_HEIGHT / 2;
		int x2 = getX() + width / 2;
		int y2 = getY() + FILE_HEIGHT / 2;
		
		boolean inWidth = (xx >= x1) && (xx <= x2);
		boolean inHeight = (yy >= y1) && (yy <= y2);

		return inWidth && inHeight;
	}
	
	public boolean isMouseOverElem(int xx, int yy, GraphicElement elem) {
		if ( Utils.isCommand( elem ) ) {
			return isMouseOverCommand(xx, yy);		
		}
		if ( Utils.isBioFile( elem ) ) {
			return isMouseOverFile(xx, yy);	
		}
		return false;
	}

	public void checkCommandXY(int xx, int yy) {
		if (xx - CANVAS_PADDING < COMMAND_WIDTH / 2) { // vlavo
			xx = COMMAND_WIDTH / 2 + CANVAS_PADDING;
		}
		if (xx + CANVAS_PADDING > CANVAS_WIDTH - COMMAND_WIDTH / 2) { // vpravo
			xx = CANVAS_WIDTH - COMMAND_HEIGHT / 2 - CANVAS_PADDING;
		}
		if (yy + CANVAS_PADDING > CANVAS_HEIGHT - COMMAND_HEIGHT / 2) { // dole
			yy = CANVAS_HEIGHT - COMMAND_HEIGHT / 2 - CANVAS_PADDING;
		}
		if (yy - CANVAS_PADDING < COMMAND_HEIGHT / 2 + CANVAS_OFFSET_Y) // hore
		{
			yy = COMMAND_HEIGHT / 2 + CANVAS_OFFSET_Y + CANVAS_PADDING;
		}
		this.setXY(xx, yy);
	}
	
	public void checkFileXY(int xx, int yy) {
		if (xx - CANVAS_PADDING < width / 2) { // vlavo
			xx = width / 2 + CANVAS_PADDING;
		}
		if (xx + CANVAS_PADDING > CANVAS_WIDTH - width / 2) { // vpravo
			xx = CANVAS_WIDTH - width / 2 - CANVAS_PADDING;
		}
		if (yy + CANVAS_PADDING > CANVAS_HEIGHT - FILE_HEIGHT / 2) { // dole
			yy = CANVAS_HEIGHT - FILE_HEIGHT / 2 - CANVAS_PADDING;
		}
		if (yy - CANVAS_PADDING < FILE_HEIGHT / 2 + CANVAS_OFFSET_Y // hore
		) {
			yy = FILE_HEIGHT / 2 + CANVAS_OFFSET_Y + CANVAS_PADDING;
		}
		this.setXY(xx, yy);
	}	
	
	
	public void performDoubleClick(){}
	
	public void addToXML(Document doc , Element root) throws ParserConfigurationException, TransformerException{}
	public static GraphicElement loadFromXML(Node cmdNode){return null;};
}
